#include "InputGameState.h"
#include "SettingsMenuState.h"
#include "NormalText.h"
#include "BackgroundImage.h"

InputGameState::InputGameState(Configuration::Key key, const std::string& header)
        : MenuState(header), configKey(key), header(header), value(""), text(NULL)
{
}

InputGameState::~InputGameState()
{
}

void InputGameState::addMenuLines()
{
    addLine("RET. Save");
    addLine("ESC. Cancel");
    text = addInfoText(new NormalText(Position(0, 100), Configuration::get(configKey)));
}

GameState* InputGameState::handleAlphaNumericKeys(char character)
{
    value += character;
    text->setText(value);
    return this;
}

void InputGameState::save()
{
    if (!value.empty())
    {
        Configuration::set(configKey, value);
    }
    Configuration::save();
}

GameState* InputGameState::performMenuActionReturn()
{
    save();
    return goToNextState(new SettingsMenuState());
}

void InputGameState::deleteLastCharacter()
{
    if (!value.empty())
    {
        value = value.substr(0, value.size() - 1);
    }
    text->setText(value);
}

GameState* InputGameState::performMenuActionBackspace()
{
    deleteLastCharacter();
    return this;
}

GameState* InputGameState::performMenuActionEscape()
{
    return goToNextState(new SettingsMenuState());
}

GameState* InputGameState::handleEvent(const Event& event)
{
    event.visitInputGameState(this);
    return MenuState::handleEvent(event);
}

